package com.weishuang.monitor.leader.commons.tree;

import java.util.List;
import java.util.function.Function;

public class TreeFunctionImpl<T> implements TreeFunction {

    /**
     * 用于存放ID列。
     */
    private Function<? super T,String> idColumn;
    /**
     * 用于存放父级ID列
     */
    private Function<? super T,String> parentColumn;

    /**
     * 用于改变选中值
     */
    private Function<? super T,Boolean> checkedColumn;

    /**
     * 用于存放子级数据列
     */
    private Function<? super T,List<T>> childrenColumn;

    /**
     * 存放处理树方法
     */
    private TreeUtils<T> tTreeUtils;

    /**
     * 父级ID，用于生成树时
     */
    private String parentId;

    /**
     * 选中的Id
     */
    private List<String> checkedIds;

    public TreeFunctionImpl(TreeUtils<T> tTreeUtils) {
        this.tTreeUtils = tTreeUtils;
    }

    //处理数据变成树，交给TreeUtils去处理（可以是个接口，根据不同实现去实现不同的树）
    @Override
    public List<T> buildTree(List list) {
        //判断必要数据是否都有
        if (idColumn == null || parentColumn == null || childrenColumn == null || tTreeUtils == null) {
            throw new RuntimeException("生成树参数不足！");
        }
        return tTreeUtils.build(list,this);
    }

    @Override
    public Function<? super T,String> getIdColumn() {
        return idColumn;
    }

    @Override
    public Function<? super T,String> getParentColumn() {
        return parentColumn;
    }

    @Override
    public Function<? super T,Boolean> getCheckedColumn(){
        return checkedColumn;
    }

    @Override
    public Function<? super T,List<T>> getChildrenAction() {
        return childrenColumn;
    }

    public TreeFunctionImpl<T> setIdColumn(Function<? super T,String> idColumn) {
        this.idColumn = idColumn;
        return this;
    }

    public TreeFunctionImpl<T> setParentColumn(Function<? super T,String> parentColumn) {
        this.parentColumn = parentColumn;
        return this;
    }

    public TreeFunctionImpl<T> setChildrenColumn(Function<? super T,List<T>> childrenColumn) {
        this.childrenColumn = childrenColumn;
        return this;
    }

    public String getParentId() {
        return parentId;
    }

    public TreeFunctionImpl<T> setParentId(String parentId) {
        this.parentId = parentId;
        return this;
    }

    public List<String> getCheckedIds() {
        return checkedIds;
    }

    /**
     * 使用这个方法时，应该添加一个设置选中的方法。无入参，返回参数是Boolean
     * @param checkedColumn
     * @param checkedIds
     * @return
     */
    public TreeFunctionImpl<T> setCheckedIds(Function<? super T,Boolean> checkedColumn,List<String> checkedIds) {
        this.checkedColumn = checkedColumn;
        this.checkedIds = checkedIds;
        return this;
    }
}
